<% layout('/layouts/default.html', {title: '出库信息管理', libs: ['validate', 'dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(ownOutInformation.isNewRecord ? '新增出库信息' : '编辑出库信息')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${ownOutInformation}" action="${ctx}/own/ownOutInformation/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="id"/>
				<!--<#form:hidden id="ownMedicineReceiptInformationId" path="ownMedicineReceiptInformationId"/>-->
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('企业名称')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="processingName" maxlength="50" class="form-control required"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('出库时间')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="time" readonly="true" maxlength="20" class="form-control Wdate"
									dataFormat="datetime" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:true});"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('新货主企业名称')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="newCorpName" maxlength="50" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('新货主企业编码')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="newCorpCode" maxlength="50" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<!--<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('出库总重量(kg)')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="weightAll" class="form-control number"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('合计金额(元)')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="outBatchAmount" class="form-control number"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('出库对象')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:radio path="outType" dictType="own_out_type" class="form-control required" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('药材/饮片批次号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:listselect id="batchId" title="药材/饮片批次号" labelPath="batchId"
								url="${ctx}/own/ownMedicineAssignment/findByKey" allowClear="false" checkbox="false"
								itemCode="medicineBatchId" itemName="medicineBatchId" class="form-control required"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('规格')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:radio path="spec" dictType="own_out_spec" class="form-control" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('出库总重量小计(kg)')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="batchWeightAll" class="form-control number"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('单价')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="unitprice" class="form-control number"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('商品编码')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="sourceCode" maxlength="50" class="form-control"/>
							</div>
						</div>
					</div>
				</div>-->
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="255" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<h4 class="form-unit">添加需要出库的信息</h4>
				<div class="ml10 mr10">
					<table id="testDataChildDataGrid"></table>
					<% if (hasPermi('own:ownOutInformation:edit')){ %>
					<a href="#" id="testDataChildDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> 增行</a>
					<% } %>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('own:ownOutInformation:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
	//初始化测试数据子表DataGrid对象
	$("#testDataChildDataGrid").dataGrid({
		data: ${toJson([])},
		datatype: "local", // 设置本地数据
		autoGridHeight: function(){return 'auto'}, // 设置自动高度

		// 设置数据表格列
		columnModel: [
			{header:'药材收获ID', name:'ownMedicineReceiptInformationId', editable:true, hidden:true},
			/*{header:'药材批次号', name:'batchId', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'}},*/
			{header:'药材批次号', name:'batchId', width:150,
				formatter: function(val, obj, row, act){
					return js.val(row, 'batchId');
				}, editable: true, edittype: "custom", editoptions: {
					custom_element: function(val, editOptions) {
						return js.template('listselectTpl', {
							id: editOptions.id, title: '药材批次号',
							name: 'batchId', value: val,
							labelName: 'batchId', labelValue: val,
							url: '${ctx}/own/ownMedicineAssignment/findByKey',
							itemCode: "medicineBatchId",itemName: "medicineBatchId",cssClass:"form-control required"
						});
					}
				}
			},
			{header:'追溯码', name:'bmTraceCode', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'25', 'class':'form-control', 'rows':'1'}},
			{header:'商品编码', name:'sourceCode', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'25', 'class':'form-control', 'rows':'1'}},
			{header:'出库总重量(kg)', name:'weightAll', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control number', 'rows':'1'}},
			{header:'出库总重量小计(kg)', name:'batchWeightAll', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control number', 'rows':'1'}},
			{header:'单价(元)', name:'unitprice', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control number', 'rows':'1'}},
			{header:'合计金额(元)', name:'outBatchAmount', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control number', 'rows':'1'}},
			{header:'出库对象', name:'outType',  width:150, editable:true, edittype:'text', editoptions:{'maxlength':'25', 'class':'form-control', 'rows':'1'}},
			{header:'规格', name:'spec', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control', 'rows':'1'}},
			{header:'操作', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
					var actions = [];
					if (val == 'new'){
						actions.push('<a href="#" onclick="js.confirm(\'你确认要删除这条数据吗？\', function(){$(\'#testDataChildDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
					}else{
						actions.push('<a href="#" onclick="js.confirm(\'你确认要删除这条数据吗？\', function(){$(\'#testDataChildDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'})});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
					}
					return actions.join('');
				}, editoptions: {defaultValue: 'new'}}
		],

		// 编辑表格参数
		editGrid: true,				// 是否是编辑表格
		editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
		editGridAddRowBtn: $('#testDataChildDataGridAddRowBtn'),	// 子表增行按钮
		editGridAddRowInitData: {},	// 新增行的时候初始化的数据

		// 编辑表格的提交数据参数
		editGridInputFormListName: 'testDataChildList', // 提交的数据列表名
		editGridInputFormListAttrs: 'ownMedicineReceiptInformationId,weightAll,batchWeightAll,unitprice,outBatchAmount,outType,spec,', // 提交数据列表的属性字段

		// 加载成功后执行事件
		ajaxSuccess: function(data){

		}
	});
</script>
<script id="listselectTpl" type="text/template">//<!--<div>
<#form:listselect id="{{d.id}}" title="{{d.title}}" name="{{d.name}}" value="{{d.value}}"
	labelName="{{d.labelName}}" labelValue="{{d.labelValue}}" url="{{d.url}}"
	itemCode="{{d.itemCode}}" itemName="{{d.itemName}}"
	class="{{d.cssClass}}" btnClass="btn-sm" allowClear="true"/>
</div>//--></script>
<script>
	var childData = [];
	$("#inputForm").validate({
		submitHandler: function(form){
			let ids = $('#testDataChildDataGrid').dataGrid('getDataIDs');
			childData = [];
			for (let id of ids){
				var obj = {
					'ownMedicineReceiptInformationId': $('#'+id+'_ownMedicineReceiptInformationId').val(),
					'batchId': $('#'+id+'_batchIdCode').val(),
					'bmTraceCode': $('#'+id+'_bmTraceCode').val(),
					'sourceCode':$('#'+id+'_sourceCode').val(),
					'weightAll': $('#'+id+'_weightAll').val(),
					'batchWeightAll': $('#'+id+'_batchWeightAll').val(),
					'outType': $('#'+id+'_outType').val(),
					'spec': $('#'+id+'_spec').val(),
					'outBatchAmount': $('#'+id+'_outBatchAmount').val(),
					'unitprice': $('#'+id+'_unitprice').val()
				};
				let temp = false;//判断是否为空对象
				for (let key in obj){
					if (obj[key]){
						temp = true;
						break;
					}
				}
				if (temp){
					childData.push(obj);
				}
			}
			if (childData.length > 0 ){
				var submitdata = {
					"processingName": $("#processingName").val(),
					"time": $("#time").val(),
					"newCorpName": $("#newCorpName").val(),
					"newCorpCode": $("#newCorpCode").val(),
					"type": "0",
					"parentId": "",
					"remarks": $("#remarks").val(),
					"batchId": childData.length,
					"outType": "-",
					"ownMedicineReceiptInformationId": "-",
					"bmTraceCode": "-",
					"sourceCode": "-",
					"weightAll": 0,
					"batchWeightAll": 0,
					"spec": "-",
					"outBatchAmount": 0,
					"unitprice": 0
				};

				$.ajax({
					url: "${ctx}/own/ownOutInformation/save",
					type: "post",
					dataType: "json",
					data: submitdata,
					success: function(data){
						if (data.result && data.data.id){
							for (let item of childData) {
								submitdata["type"] = "1";
								submitdata["remarks"] = "";
								submitdata["parentId"] = data.data.id;
								submitdata["batchId"] = item["batchId"];
								submitdata["outType"] = item["outType"];
								submitdata["ownMedicineReceiptInformationId"] = item["ownMedicineReceiptInformationId"];
								submitdata["bmTraceCode"] = item["bmTraceCode"];
								submitdata["sourceCode"] = item["sourceCode"];
								submitdata["weightAll"] = item["weightAll"];
								submitdata["batchWeightAll"] = item["batchWeightAll"];
								submitdata["spec"] = item["spec"];
								submitdata["outBatchAmount"] = item["outBatchAmount"];
								submitdata["unitprice"] = item["unitprice"];
								$.ajax({
									url: "${ctx}/own/ownOutInformation/save",
									type: "post",
									dataType: "json",
									data: submitdata,
									success: function(data){
										if (!data.result || !data.data.id){
											js.showErrorMessage("出库失败!");
										}
									},
									error: function(e){
										js.showErrorMessage("错误",e);
									}
								})
							}
							js.closeCurrentTabPage(function(contentWindow){
								contentWindow.page();
							});
						}else {
							js.showErrorMessage("出库失败!");
						}
					},
					error: function(e){
						js.showErrorMessage("错误",e);
					}
				})
			}else {
				js.showErrorMessage("请至少添加一项出库信息！");
			}
		}
	});
	//选择药材批次号回调方法
	function  listselectCallback(id, act, index, layero, selectData) {
		if (act == 'ok' || act == 'clear'){
			let temp = id.split("_")[0];
			for (let key in selectData){
				let data = selectData[key];
				$('#'+temp+'_ownMedicineReceiptInformationId').val(data['ownMedicineReceiptInformationId']);
				$('#'+temp+'_bmTraceCode').val(data['bmTraceCode']);
				$('#'+temp+'_sourceCode').val(data['sourceCode']);
				$('#'+temp+'_weightAll').val(data['weight']);
				$('#'+temp+'_batchWeightAll').val(data['weight']);
				$('#'+temp+'_outType').val("medicine");
				$('#'+temp+'_spec').val(data['spec']);
				break;
			}
		}
	}
	function close(){
		$("input[name='bmTraceCode']").attr("readonly","readonly");
		$("input[name='sourceCode']").attr("readonly","readonly");
		$("input[name='weightAll']").attr("readonly","readonly");
		$("input[name='batchWeightAll']").attr("readonly","readonly");
		$("input[name='outType']").attr("readonly","readonly");
		$("input[name='spec']").attr("readonly","readonly");
	}
	$(function(){
		close();
		var jsss = "${ownOutInformation.isNewRecord}"; //获取beetl的值
		if (jsss == 'false')
			$("#batchIdName").attr("disabled","disabled");
		$('#testDataChildDataGridAddRowBtn').click(function (){
			close();
		})
	});
</script>